﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EBEInventory_DAL;
public partial class Inventory_Loc_SubLoc_Mangement_Default : System.Web.UI.Page
{
    InventoryEntities ctx = null;
    bool done = false;
    protected void Page_Load(object sender, EventArgs e)
    {
         ctx = InitConnection.Initialize();
        if (!IsPostBack)
        {
            string opType = Server.UrlDecode(Request.QueryString["Operation"]);

            if (string.Compare(opType, "Register") == 0 || string.Compare(opType, "Delete") == 0 || string.Compare(opType, "Edit") == 0)
            {
                ViewState["Operation"] = btn_SubLocRegister.Text = opType;

                Dictionary<int[], string[]> dic = InvOps.GetInvNames(ctx);

                int i = 0;
                while (i < dic.Keys.ToArray()[0].Length)
                {
                    ListItem li = new ListItem(dic.Values.ToArray()[0][i], dic.Keys.ToArray()[0][i].ToString());
                    dropDown_InvID.Items.Add(li);

                    i++;
                }

                Sublocation sub = (Sublocation)Session["SelectedSubLoc"];
                if (sub != null)
                {
                    dropDown_InvID.SelectedValue = sub.InvID.ToString();
                    HiddenID.Value = sub.ID.ToString();
                    txt_SubLocName.Text = sub.Name;
                    txt_SubLocNote.Text = sub.Notes;
                }
            }
            Page.DataBind();
        }
       
        
    }
    protected void btn_SubLocRegister_Click(object sender, EventArgs e)
    {
        Dictionary<string, string> sub = new Dictionary<string, string>();
        sub["InvID"] = dropDown_InvID.SelectedValue;
        sub["ID"] = HiddenID.Value.ToString();
        sub["Name"] = txt_SubLocName.Text;
        sub["Notes"] = txt_SubLocNote.Text;
        try
        {
            if (string.Compare(ViewState["Operation"].ToString(), "Register") == 0)
            {
                SubInvOps.AddSubInventory(sub, ctx);
                done = true;
            }

            else if (string.Compare(ViewState["Operation"].ToString(), "Edit") == 0)
            {
                SubInvOps.EditSubInventory(sub, ctx);
                done = true;
            }

            else if (string.Compare(ViewState["Operation"].ToString(), "Delete") == 0)
            {
                SubInvOps.DeleteSubInventory(sub, ctx);
                done = true;
            }
        }

        catch (Exception ex)
        {
            lbl_err.Text = ex.Message;
            done = false;
        }
        

        if(done)
        Response.Redirect("~/Inventory/Loc-SubLoc Mangement/ListLoc-Sub.aspx");
    }
}